package cn.qh.test01.controller;

import cn.qh.test01.dto.DepartmentDTO;
import cn.qh.test01.entity.Department;
import cn.qh.test01.exception.NotFoundException;
import cn.qh.test01.service.DepartmentService;
import cn.qh.test01.vo.CommonResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import io.swagger.annotations.ApiImplicitParam;
import org.springframework.beans.BeanUtils;

/**
 * (Department)控制层
 *
 * @author makejava
 * @since 2021-10-26 17:33:48
 */

@RestController
@RequestMapping("department")
@Api
public class DepartmentController {
    /**
     * 服务对象
     */
    @Resource
    private DepartmentService departmentService;

    /**
     * 分页查询所有数据
     *
     * @param current 分页对象
     * @return 所有数据
     */
    @GetMapping("/list")
    @ApiOperation(value = "分页查询所有数据", produces =
            "application/json;charset=utf-8",
            httpMethod = "GET", response = CommonResponse.class)
    @ApiImplicitParam(name = "current", value = "当前页", paramType =
            "query", dataType = "int")
    @CrossOrigin
    public Page<Department> selectAll(@RequestParam(defaultValue = "1") int
                                              current) {
        Page page = new Page();
        page.setCurrent(current);
        page.setSize(3);
        this.departmentService.page(page);
        if (1 == 1) {
            throw new NotFoundException(1001);
        }
        return page;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @CrossOrigin
    @ApiOperation(value = "通过主键查询单条数据", produces =
            "application/json;charset=utf-8",
            httpMethod = "GET", response = CommonResponse.class)
    @ApiImplicitParam(name = "id", value = "主键", paramType = "path", dataType =
            "int")
    public Department selectOne(@PathVariable Serializable id) {
        return this.departmentService.getById(id);
    }

    /**
     * 新增数据
     *
     * @param departmentDTO 实体对象
     * @return 新增结果
     */
    @PostMapping("/insert")
    @ApiOperation(value = "新增", produces = "application/json;charset=utf-8",
            httpMethod = "POST", response = CommonResponse.class)
    @CrossOrigin
    public boolean insert(@RequestBody DepartmentDTO departmentDTO) {
        Department department = new Department();
        BeanUtils.copyProperties(departmentDTO, department);
        return this.departmentService.save(department);
    }

    /**
     * 修改数据
     * 数据访问侧的模版 dao.java.vm
     *
     * @param departmentDTO 实体对象
     * @return 修改结果
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改信息", produces = "application/json;charset=utf-8",
            httpMethod = "PUT", response = CommonResponse.class)
    @CrossOrigin
    public boolean update(@RequestBody DepartmentDTO departmentDTO) {
        Department department = new Department();
        BeanUtils.copyProperties(departmentDTO, department);
        return this.departmentService.updateById(department);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "根据主键删除信息", produces =
            "application/json;charset=utf-8",
            httpMethod = "DELETE", response = CommonResponse.class)
    @ApiImplicitParam(name = "id", value = "主键", paramType = "path", dataType =
            "int")
    @CrossOrigin
    public boolean delete(@PathVariable Serializable id) {
        return this.departmentService.removeById(id);
    }
}

